package com.shop.cereshop.app.dl.logic.controller;

import com.shop.cereshop.app.dl.logic.service.CerePlatformUserPointService;
import com.shop.cereshop.app.dl.logic.utils.JS_Sign;
import com.shop.cereshop.app.dl.logic.utils.ResultInfo;
import com.shop.cereshop.app.param.order.OrderParam;
import com.shop.cereshop.app.redis.service.api.StringRedisService;
import com.shop.cereshop.app.service.buyer.CereBuyerUserService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.utils.AppletPayUtil;
import com.shop.cereshop.commons.utils.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: dlshop
 * 描述：
 * @author: 我好帅
 * @create: 2021-06-15 17:14
 **/
@RestController
@RequestMapping("/api")
@CrossOrigin
public class CerePlatformUserPointController {
    @Value("${weixin.appid}")
    private String appId;
    @Value("${weixin.secret}")
    private String secret;
    @Autowired
    private CerePlatformUserPointService cerePlatformUserPointService;
    @Autowired
    private StringRedisService stringRedisService;
    @Autowired
    private CereBuyerUserService cereBuyerUserService;

    //转发得积分
    @PostMapping("/transpondGetIntegral")
    public ResultInfo<?> transpondGetIntegral(HttpServletRequest request,Long buyerUserId, Integer newsId,String time) {
        return cerePlatformUserPointService.transpondGetIntegral(buyerUserId, newsId,time);
    }

    //积分明细Points details
    @PostMapping("/pointsDetails")
    public ResultInfo<?> pointsDetails(HttpServletRequest request, @RequestParam(value = "page", defaultValue = "1") Integer page,
                                    @RequestParam(value = "row", defaultValue = "10") Integer row) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        if(user == null){
            throw new CoBusinessException(CoReturnFormat.USER_NOT_LOGIN);
        }
        return cerePlatformUserPointService.getAllByUserid(page, row, user.getBuyerUserId());
    }

    //积分兑换记录Points redemption record
    @PostMapping("/pointsRedemptionRecord")
    public ResultInfo<?> pointsRedemptionRecord(HttpServletRequest request, @RequestParam(value = "page", defaultValue = "1") Integer page,
                                             @RequestParam(value = "row", defaultValue = "10") Integer row) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
//        CereBuyerUser user=new CereBuyerUser();
//        user.setBuyerUserId(876l);
        if(user == null){
            throw new CoBusinessException(CoReturnFormat.USER_NOT_LOGIN);
        }
        return cerePlatformUserPointService.pointsRedemptionRecord(page, row, user.getBuyerUserId());
    }

    /**
     * 获取微信签名
     * 需要在公众号上配置js安全域名
     */
    @RequestMapping(value = "/weiSign", method = {
            RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public ResultInfo<?> wapSignSignatureAction(String url) {
        Map<String, String> jsSignMapResult = JS_Sign.getJSSignMapResult(appId, secret, url, stringRedisService);
        return new ResultInfo<>(jsSignMapResult);
    }

    //积分兑换商品Points to redeem goods
    @PostMapping("/pointsToRedeemGoods")
    public ResultInfo<?> pointsToRedeemGoods( OrderParam param,HttpServletRequest request) throws CoBusinessException {
        String token = request.getHeader("Authorization");
        CereBuyerUser user=null;
        if(!EmptyUtils.isEmpty(token)){
            //根据token查询用户信息
            user=cereBuyerUserService.findByToken(token);
        }
        String ip= AppletPayUtil.getClientIp(request);
        if(user == null){
            throw new CoBusinessException(CoReturnFormat.USER_NOT_LOGIN);
        }
//        CereBuyerUser user= new CereBuyerUser();
//        user.setBuyerUserId(876l);
        return cerePlatformUserPointService.pointsToRedeemGoods(param,user,ip);
    }



}
